import allure
from selenium import webdriver

import yaml

from src.day36.pages.base_page import BasePage
from src.day36.pages.main_page import MainPage
from src.day36.utils import root_dir


class LoginPage(BasePage):
    __LOGIN_URL = "https://work.weixin.qq.com/wework_admin/loginpage_wx"

    def login_by_cookies(self):
        """
        通过cookies进行登录
        :return:
        """
        with allure.step("企业登录"):
            self.driver.get(self.__LOGIN_URL)
            # 2. 读取Cookie信息
            cookies = self.read_cookies_from_file()
            # 3. 把Cookie信息添加到浏览器
            for cookie in cookies:
                self.driver.add_cookie(cookie)
            # 4. 刷新页面
            self.driver.refresh()
        return MainPage(self.driver)

    def read_cookies_from_file(self):
        with open(f"{root_dir}/data/cookies.yaml", encoding="utf-8") as f:
            cookies = yaml.safe_load(f)
            return cookies
